home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / ByteArrayOutputStream.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  7.2 KB  |  228 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)ByteArrayOutputStream.java    1.39 98/09/30
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * This class implements an output stream in which the data is 
  20.  * written into a byte array. The buffer automatically grows as data 
  21.  * is written to it. 
  22.  * The data can be retrieved using <code>toByteArray()</code> and
  23.  * <code>toString()</code>.
  24.  *
  25.  * @author  Arthur van Hoff
  26.  * @version 1.27, 10/20/97
  27.  * @since   JDK1.0
  28.  */
  29.  
  30. public class ByteArrayOutputStream extends OutputStream {
  31.  
  32.     /** 
  33.      * The buffer where data is stored. 
  34.      */
  35.     protected byte buf[];
  36.  
  37.     /**
  38.      * The number of valid bytes in the buffer. 
  39.      */
  40.     protected int count;
  41.  
  42.     /**
  43.      * Flag indicating whether the stream has been closed.
  44.      */
  45.     private boolean isClosed = false;
  46.  
  47.     /** Check to make sure that the stream has not been closed */
  48.     private void ensureOpen() {
  49.         /* This method does nothing for now.  Once we add throws clauses
  50.      * to the I/O methods in this class, it will throw an IOException
  51.      * if the stream has been closed.
  52.      */
  53.     }
  54.  
  55.     /**
  56.      * Creates a new byte array output stream. The buffer capacity is 
  57.      * initially 32 bytes, though its size increases if necessary. 
  58.      */
  59.     public ByteArrayOutputStream() {
  60.     this(32);
  61.     }
  62.  
  63.     /**
  64.      * Creates a new byte array output stream, with a buffer capacity of 
  65.      * the specified size, in bytes. 
  66.      *
  67.      * @param   size   the initial size.
  68.      * @exception  IllegalArgumentException if size is negative.
  69.      */
  70.     public ByteArrayOutputStream(int size) {
  71.         if (size < 0) {
  72.             throw new IllegalArgumentException("Negative initial size: "
  73.                                                + size);
  74.         }
  75.     buf = new byte[size];
  76.     }
  77.  
  78.     /**
  79.      * Writes the specified byte to this byte array output stream. 
  80.      *
  81.      * @param   b   the byte to be written.
  82.      */
  83.     public synchronized void write(int b) {
  84.     ensureOpen();
  85.     int newcount = count + 1;
  86.     if (newcount > buf.length) {
  87.         byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
  88.         System.arraycopy(buf, 0, newbuf, 0, count);
  89.         buf = newbuf;
  90.     }
  91.     buf[count] = (byte)b;
  92.     count = newcount;
  93.     }
  94.  
  95.     /**
  96.      * Writes <code>len</code> bytes from the specified byte array 
  97.      * starting at offset <code>off</code> to this byte array output stream.
  98.      *
  99.      * @param   b     the data.
  100.      * @param   off   the start offset in the data.
  101.      * @param   len   the number of bytes to write.
  102.      */
  103.     public synchronized void write(byte b[], int off, int len) {
  104.     ensureOpen();
  105.     if ((off < 0) || (off > b.length) || (len < 0) ||
  106.             ((off + len) > b.length) || ((off + len) < 0)) {
  107.         throw new IndexOutOfBoundsException();
  108.     } else if (len == 0) {
  109.         return;
  110.     }
  111.         int newcount = count + len;
  112.         if (newcount > buf.length) {
  113.             byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
  114.             System.arraycopy(buf, 0, newbuf, 0, count);
  115.             buf = newbuf;
  116.         }
  117.         System.arraycopy(b, off, buf, count, len);
  118.         count = newcount;
  119.     }
  120.  
  121.     /**
  122.      * Writes the complete contents of this byte array output stream to 
  123.      * the specified output stream argument, as if by calling the output 
  124.      * stream's write method using <code>out.write(buf, 0, count)</code>.
  125.      *
  126.      * @param      out   the output stream to which to write the data.
  127.      * @exception  IOException  if an I/O error occurs.
  128.      */
  129.     public synchronized void writeTo(OutputStream out) throws IOException {
  130.     out.write(buf, 0, count);
  131.     }
  132.  
  133.     /**
  134.      * Resets the <code>count</code> field of this byte array output 
  135.      * stream to zero, so that all currently accumulated output in the 
  136.      * ouput stream is discarded. The output stream can be used again, 
  137.      * reusing the already allocated buffer space. 
  138.      *
  139.      * @see     java.io.ByteArrayInputStream#count
  140.      */
  141.     public synchronized void reset() {
  142.     ensureOpen();
  143.     count = 0;
  144.     }
  145.  
  146.     /**
  147.      * Creates a newly allocated byte array. Its size is the current 
  148.      * size of this output stream and the valid contents of the buffer 
  149.      * have been copied into it. 
  150.      *
  151.      * @return  the current contents of this output stream, as a byte array.
  152.      * @see     java.io.ByteArrayOutputStream#size()
  153.      */
  154.     public synchronized byte toByteArray()[] {
  155.     byte newbuf[] = new byte[count];
  156.     System.arraycopy(buf, 0, newbuf, 0, count);
  157.     return newbuf;
  158.     }
  159.  
  160.     /**
  161.      * Returns the current size of the buffer.
  162.      *
  163.      * @return  the value of the <code>count</code> field, which is the number
  164.      *          of valid bytes in this output stream.
  165.      * @see     java.io.ByteArrayOutputStream#count
  166.      */
  167.     public int size() {
  168.     return count;
  169.     }
  170.  
  171.     /**
  172.      * Converts the buffer's contents into a string, translating bytes into
  173.      * characters according to the platform's default character encoding.
  174.      */
  175.     public String toString() {
  176.     return new String(buf, 0, count);
  177.     }
  178.  
  179.     /**
  180.      * Converts the buffer's contents into a string, translating bytes into
  181.      * characters according to the specified character encoding.
  182.      *
  183.      * @param   enc  a character-encoding name.
  184.      * @since   JDK1.1
  185.      */
  186.     public String toString(String enc) throws UnsupportedEncodingException {
  187.     return new String(buf, 0, count, enc);
  188.     }
  189.  
  190.     /**
  191.      * Creates a newly allocated string. Its size is the current size of 
  192.      * the output stream and the valid contents of the buffer have been 
  193.      * copied into it. Each character <i>c</i> in the resulting string is 
  194.      * constructed from the corresponding element <i>b</i> in the byte 
  195.      * array such that:
  196.      * <blockquote><pre>
  197.      *     c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
  198.      * </pre></blockquote>
  199.      *
  200.      * @deprecated This method does not properly convert bytes into characters.
  201.      * As of JDK 1.1, the preferred way to do this is via the
  202.      * <code>toString(String enc)</code> method, which takes an encoding-name
  203.      * argument, or the <code>toString()</code> method, which uses the
  204.      * platform's default character encoding.
  205.      *
  206.      * @param      hibyte    the high byte of each resulting Unicode character.
  207.      * @return     the current contents of the output stream, as a string.
  208.      * @see        java.io.ByteArrayOutputStream#size()
  209.      * @see        java.io.ByteArrayOutputStream#toString(String)
  210.      * @see        java.io.ByteArrayOutputStream#toString()
  211.      */
  212.     public String toString(int hibyte) {
  213.     return new String(buf, hibyte, 0, count);
  214.     }
  215.  
  216.     /**
  217.      * Closes this output stream and releases any system resources 
  218.      * associated with this stream. A closed stream cannot perform 
  219.      * output operations and cannot be reopened.
  220.      * <p>
  221.      *
  222.      */
  223.     public synchronized void close() throws IOException {
  224.     isClosed = true;
  225.     }
  226.  
  227. }
  228.